package me.yiyou.meihua.ui.page.random

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.google.gson.Gson
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.yiyou.meihua.data.SaveBean
import me.yiyou.meihua.element.CustomButton
import me.yiyou.meihua.ui.screen.Screen
import java.net.URLEncoder

@Composable
fun RandomPrePage(navController: NavController, viewModel: RandomViewModel = viewModel()) {

    val content by viewModel.inputContent

    Row(
        Modifier
            .fillMaxWidth()
            .padding(top = 180.dp, start = 20.dp, end = 20.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        Column(
            Modifier.fillMaxWidth(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(text = "请输入占问事")
            OutlinedTextField(
                value = content,
                onValueChange = viewModel::onInputContentChange,
                label = { Text("占问事") },
                placeholder = { Text("请输入占问事") },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
            )
            Row(
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.fillMaxWidth()
            ) {
                CustomButton(
                    text = "随机数生成",
                    onClick = {
                        viewModel.randomNumber()
                        var saveBean = SaveBean(
                            shang = viewModel.number1,
                            xia = viewModel.number2,
                            dong = viewModel.dongyao,
                            content = viewModel.inputContent.value,
                            time = viewModel.time
                        )
                        gotoRandom(navController, saveBean, viewModel)
                    },
                    modifier = Modifier.width(180.dp),
                    enabled = !viewModel.state
                )
            }
            Text(
                text = "点击生成随机数,生成两秒后跳转",
                modifier = Modifier
                    .padding(20.dp)
                    .align(Alignment.CenterHorizontally)
            )
            if (viewModel.state) {
                Row(
                    modifier = Modifier
                        .padding(20.dp)
                        .fillMaxWidth(),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(text = "${viewModel.number1}", modifier = Modifier.padding(20.dp))
                    Text(text = "${viewModel.number2}", modifier = Modifier.padding(20.dp))
                }
            }
        }
    }
}

/**
 * 跳转页面
 */
@OptIn(DelicateCoroutinesApi::class)
fun gotoRandom(navController: NavController, saveBean: SaveBean, viewModel: RandomViewModel) {
    GlobalScope.launch {
        withContext(Dispatchers.Main) {
            delay(2000)
            val json = Gson().toJson(saveBean) // 序列化为 JSON
            val encodedJson = URLEncoder.encode(json, "UTF-8")
            navController.navigate("${Screen.Random.route}/${encodedJson}")
        }
        withContext(Dispatchers.IO) {
            viewModel.insert(saveBean)
        }
    }
}
